# 存储学生成绩的列表，每个元素是一个字典，包含学生姓名、学号、课程、成绩等信息
student_scores = []


# 记录学生成绩的函数
def record_score():
    print("====== 记录学生成绩 ======")
    name = input("请输入学生姓名：")
    student_id = input("请输入学生学号：")
    course = input("请输入课程名称：")
    while True:
        try:
            score = float(input("请输入成绩（0-100）："))
            if 0 <= score <= 100:
                break
            else:
                print("成绩必须在0到100之间，请重新输入。")
        except ValueError:
            print("输入无效，请输入一个有效的数字成绩。")
    student_scores.append({"name": name, "student_id": student_id, "course": course, "score": score})
    print("成绩已成功记录!")


# 查询学生成绩的函数
def query_score():
    print("====== 查询学生成绩 ======")
    print("请选择查询方式：")
    print("1. 按学生姓名查询")
    print("2. 按学生学号查询")
    print("3. 按课程名称查询")
    choice = input("请输入选项序号：")
    if choice == "1":
        name = input("请输入学生姓名：")
        found = False
        for score_info in student_scores:
            if score_info["name"] == name:
                print(f"姓名：{score_info['name']}, 学号：{score_info['student_id']}, 课程：{score_info['course']}, 成绩：{score_info['score']}")
                found = True
        if not found:
            print(f"没有找到{name}的成绩记录")
    elif choice == "2":
        student_id = input("请输入学生学号：")
        found = False
        for score_info in student_scores:
            if score_info["student_id"] == student_id:
                print(f"姓名：{score_info['name']}, 学号：{score_info['student_id']}, 课程：{score_info['course']}, 成绩：{score_info['score']}")
                found = True
        if not found:
            print(f"没有找到学号为{student_id}的成绩记录")
    elif choice == "3":
        course = input("请输入课程名称：")
        found = False
        for score_info in student_scores:
            if score_info["course"] == course:
                print(f"姓名：{score_info['name']}, 学号：{score_info['student_id']}, 课程：{score_info['course']}, 成绩：{score_info['score']}")
                found = True
        if not found:
            print(f"没有找到{course}课程的成绩记录")
    else:
        print("无效的选择，请重新输入")


# 统计课程成绩的函数
def statistics_course_score():
    print("====== 统计课程成绩 ======")
    course = input("请输入课程名称：")
    scores = [record["score"] for record in student_scores if record["course"] == course]
    if scores:
        avg_score = sum(scores) / len(scores)
        max_score = max(scores)
        min_score = min(scores)
        print(f"课程：{course}")
        print(f"平均分：{avg_score:.2f}")
        print(f"最高分：{max_score}")
        print(f"最低分：{min_score}")
    else:
        print(f"没有找到{course}课程的成绩记录。")


print("=" * 50)
print("欢迎使用学生成绩管理系统")
print("=" * 50)
while True:
    print("请选择操作:")
    print("1. 记录学生成绩")
    print("2. 查询学生成绩")
    print("3. 统计课程成绩")
    print("4. 退出系统")
    choice = input("请输入选项序号：")
    if choice == "1":
        record_score()
    elif choice == "2":
        query_score()
    elif choice == "3":
        statistics_course_score()
    elif choice == "4":
        print("【退出系统界面】")
        print("感谢使用学生成绩管理系统，再见!")
        break
    else:
        print("无效的选择，请重新输入")
